package com.laowang.project.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.Sample;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.common.utils.StringUtils;
import com.laowang.common.utils.aip.CarInfo;
import com.laowang.framework.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyCyhCarMapper;
import com.laowang.project.system.domain.MyCyhCar;
import com.laowang.project.system.service.MyCyhCarService;
import oshi.jna.platform.mac.SystemB;
import sun.java2d.pipe.SpanShapeRenderer;

/**
 * 我的爱车Service业务层处理
 *
 * @author laowang
 * @date 2023-05-09
 */
@Service
public class MyCyhCarServiceImpl implements MyCyhCarService {
    @Autowired
    private MyCyhCarMapper myCyhCarMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询我的爱车
     *
     * @param id 我的爱车ID
     * @return 我的爱车
     */
    @Override
    public MyCyhCar selectMyCyhCarById(Integer id) {
        return myCyhCarMapper.selectMyCyhCarById(id);
    }

    /**
     * 查询我的爱车列表
     *
     * @param myCyhCar 我的爱车
     * @return 我的爱车
     */
    @Override
    public List<MyCyhCar> selectMyCyhCarList(MyCyhCar myCyhCar) {
        return myCyhCarMapper.selectMyCyhCarList(myCyhCar);
    }

    /**
     * 新增我的爱车
     *
     * @param myCyhCar 我的爱车
     * @return 结果
     */
    @Override
    public int insertMyCyhCar(MyCyhCar myCyhCar) {
        return myCyhCarMapper.insertMyCyhCar(myCyhCar);
    }

    /**
     * 修改我的爱车
     *
     * @param myCyhCar 我的爱车
     * @return 结果
     */
    @Override
    public int updateMyCyhCar(MyCyhCar myCyhCar) {
        return myCyhCarMapper.updateMyCyhCar(myCyhCar);
    }

    /**
     * 批量删除我的爱车
     *
     * @param ids 需要删除的我的爱车ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhCarByIds(Integer[] ids) {
        return myCyhCarMapper.deleteMyCyhCarByIds(ids);
    }

    /**
     * 删除我的爱车信息
     *
     * @param id 我的爱车ID
     * @return 结果
     */
    @Override
    public int deleteMyCyhCarById(Integer id) {
        return myCyhCarMapper.deleteMyCyhCarById(id);
    }

    @Override
    public int insertMyCyhCarWeb(MyCyhCar myCyhCar) {

        String photo = myCyhCar.getPhoto();
        if(StringUtils.isEmpty(photo)){
            throw new CustomException("行驶证图片不能为空",400);
        }

        Map<String, String> access_token = redisCache.getCacheObject("access_token");
        if (access_token == null) {
            access_token = Sample.getToken();
            String expires_in = String.valueOf(access_token.get("expires_in"));
            //System.out.println("expires_in:"+expires_in);
            redisCache.setCacheObject("access_token", access_token, Integer.valueOf(expires_in), TimeUnit.SECONDS);

        } else {
            String expires_in = String.valueOf(access_token.get("expires_in"));
            if (Integer.valueOf(expires_in) < 3600) {
                access_token = Sample.getToken();
                redisCache.setCacheObject("access_token", access_token, Integer.valueOf(expires_in), TimeUnit.SECONDS);
            }
        }

        CarInfo carInfo = Sample.vehicleLicense(photo);
        if (carInfo !=null){
            myCyhCar.setName(carInfo.getPlate_number());
            myCyhCar.setProduct(carInfo.getBrand_model());
            MyCyhCar myCyhCar1 = selectByName(myCyhCar.getName());
            if (myCyhCar1 != null){
                throw new CustomException("该车牌已经登记",400);
            }
        }

        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        myCyhCar.setUserId(mid);
        myCyhCar.setCtime(new Date());
        myCyhCar.setDengji("0");

        return myCyhCarMapper.insertMyCyhCar(myCyhCar);
    }

    @Override
    public MyCyhCar selectByName(String plate_number) {
        return myCyhCarMapper.selectByName(plate_number);
    }

    @Override
    public List<MyCyhCar> selectByUserId(Integer mid) {
        return myCyhCarMapper.selectByUserId(mid);
    }
}
